
const logger = require('../logs/logger');

let _subscriptions = [];

class SubscriptionManager {
    get subscriptions() {
        return _subscriptions;
    }

    find(id) {
        return _subscriptions.find((v, i , s) => v.id === id);
    }

    addMsg(id, msg) {
        addMsgs(id, [msg]);
    }

    addMsgs(id, msgs) {
        var subscription = this.find(id);
        if (subscription) {
            subscription.msgs = subscription.msgs.concat(msgs);
        } else {
            _subscriptions.push({
                id: id,
                msgs: msgs
            });
        }
    }

    popMsg(id) {
        var subscription = this.find(id);
        if (subscription) {
            return subscription.msgs.pop();
        }
        return null;
    }
}

module.exports = SubscriptionManager;